﻿@using Seal.Model
@using Seal.Helpers
@using SealWebServer.Models
@{
    FolderDetailModel folderDetail = Model as FolderDetailModel;
    Repository repository = folderDetail.Repository;
    SecurityUser user = folderDetail.User;
}

@helper GetDocumentImage(string extension)
{
    string extension2 = extension.Replace(".", "").ToLower();
    string result = "document.gif";
    if (extension2 == Repository.SealReportFileExtension)
    {
        result = "report.gif";
    }
    else if (extension2 == "htm" || extension2 == "pdf" || extension2 == "csv" || extension2 == "xls" || extension2 == "xlsx")
    {
        result = "document" + extension2 + ".gif";
    }
    @result
}


@helper ShowReports(string folder, bool isRecursive, Repository repository, SecurityUser user)
{
    SecurityFolder securityFolder = user.FindSecurityFolder(folder);
    if (securityFolder == null && !user.IsParentSecurityFolder(folder))
    {
        return;
    }

    if (securityFolder != null)
    {
        foreach (string path in Directory.GetFiles(folder, securityFolder.UsedSearchPattern).Where(i => !Report.IsSealAttachedFile(i)))
        {
            if (Path.GetFileName(path) != securityFolder.DescriptionFile && Path.GetFileName(path) != Path.GetFileNameWithoutExtension(securityFolder.DescriptionFile) + ".cshtml")
            {
                bool isReport = Report.IsSealReportFile(path);
                <tr>
                    <td class='cell_value'>
                        <img src="~/Images/@GetDocumentImage(Path.GetExtension(path))" class="list_icon" /><a title="@repository.TranslateWeb(isReport ? "Execute the report" : "Open the file")" href="@(isReport ? "InitExecuteReport" : "ViewFile")?path=@HttpUtility.UrlEncode(path.Substring(repository.ReportsFolder.Length))" onclick="$(this).blur();" target="_blank">@repository.TranslateFileName(path)</a>
                    </td>
                    <td class='cell_value' style="text-align: center; width: 18px;">
                        @if (securityFolder.PublicationType == PublicationType.ExecuteOutput)
                        {
                            <img id="@path.Substring(repository.ReportsFolder.Length)" onclick="openViewOutputDialog(this)" src="~/Images/device.png" title="@repository.TranslateWeb("Execute report views or outputs")" style="cursor: hand;@(isReport ? "" : "display:none;")" />
                        }
                    </td>
                    <td class='cell_value' style="text-align: right;">@File.GetLastWriteTime(path).ToString("G", repository.CultureInfo)</td>
                    <td class='cell_value'>@repository.TranslateFolderPath(Path.GetDirectoryName(path))</td>
                </tr>
            }
        }
    }
    if (isRecursive)
    {
        foreach (string subFolder in Directory.GetDirectories(folder))
        {
            @ShowReports(subFolder, isRecursive, repository, user)
        }
    }
}

<!DOCTYPE html>
<html>
<head>
    <link href="Images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    @if (repository.Configuration.IsLocal)
    {
        <script type='text/javascript' src='~/Scripts/jquery-1.9.1.min.js'></script>
        <script type='text/javascript' src='~/Scripts/jquery-ui-1.10.3.min.js'></script>
        <script type='text/javascript' src='~/Scripts/jquery.dataTables.min.js'></script>
        <link href='~/Scripts/jquery-ui.css' rel='stylesheet' type='text/css' />
    }
    else
    {
        <script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.min.js'></script>
        <script type='text/javascript' src='http://code.jquery.com/ui/1.10.3/jquery-ui.min.js'></script>
        <script type='text/javascript' src='http://ajax.aspnetcdn.com/ajax/jquery.dataTables/1.9.4/jquery.dataTables.min.js'></script>
        <link href='http://codeorigin.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css' rel='stylesheet' type='text/css' />
    }
    <link href="~/Scripts/sealWeb.css" rel="styleSheet" type="text/css" />
    <style type="text/css">
    </style>

    <script type="text/javascript">
        //format for datatables
        var shortDateFormat = '@repository.CultureInfo.DateTimeFormat.ShortDatePattern';
        var isUSdate = shortDateFormat.match(/^m/i);
        var dateSeparator = '@repository.CultureInfo.DateTimeFormat.DateSeparator';
        var decimalSeparator = '@repository.CultureInfo.NumberFormat.NumberDecimalSeparator';

        function login() {
            $("#loginForm").attr("action", "Login");
            $("#loginForm").submit();
        }

        function setUserInfo() {
            $("#userInfoForm").attr("action", "SetUserInfo");
            $("#userInfoForm").submit();
        }

        $(document).ready(function () {
            $(document).tooltip();

            $("#errorDialog").dialog({
                modal: true,
                autoOpen: false,
                minWidth: 400,
                height: "auto",
                closeText: "@repository.TranslateWeb("Close")"
            });

            $(".viewOutputDialog").dialog({
                modal: true,
                autoOpen: false,
                minWidth: 500,
                height: "auto",
                closeText: "@repository.TranslateWeb("Close")"
            });

            $("#userPreferencesDialog").dialog({
                modal: true,
                autoOpen: false,
                minWidth: 600,
                width: "auto",
                height: "auto",
                closeText: "@repository.TranslateWeb("Cancel")",
                buttons: {
                    "@repository.TranslateWeb("Save")" : setUserInfo,
                    "@repository.TranslateWeb("Cancel")": function() {
                        $( this ).dialog( "close" );
                    }
                }
            });

            $("#userInfo").click(function(){ openUserPreferencesDialog();})

            dialog = $("#loginDialog").dialog({
                autoOpen: false,
                minWidth: 350,
                height: "auto",
                modal: true,
                closeText: "@repository.TranslateWeb("Close")",
                buttons: {
                    "@repository.TranslateWeb("Login")": login
                }
            });
            $('#loginDialog').keypress(function(e) {
                if (e.keyCode == $.ui.keyCode.ENTER) {
                    login();
                }
            });

            var dataTables = $('.data_table');
            try {
                if (dataTables != null && true && !false) {
                    dataTables.dataTable({
                        "sDom": '<"dataTableTop"lfpri>t',
                        "aoColumnDefs": [{ "bSortable": false, "aTargets": ["firstcell"] }],
                        "oLanguage": {
                            "sProcessing": "@repository.TranslateReport("Processing...")",
                            "sLengthMenu": "@repository.TranslateReport("Show _MENU_ reports")",
                            "sZeroRecords": "@repository.TranslateReport("No reports")",
                            "sInfo": "@repository.TranslateReport("Showing _START_ to _END_ of _TOTAL_")",
                            "sInfoEmpty": "@repository.TranslateReport("Showing 0 to 0 of 0")",
                            "sInfoFiltered": "@repository.TranslateReport("(filtered from _MAX_)")",
                            "sInfoPostFix": "",
                            "sSearch": "@repository.TranslateReport("Filter:")",
                            "oPaginate": {
                                "sFirst": "|&lt;",
                                "sPrevious": "&lt;&lt;",
                                "sNext": ">>",
                                "sLast": ">|"
                            }
                        },
                        "bSort": true,
                        "aaSorting": [],
                        "bPaginate": true,
                        "sPaginationType": "full_numbers",
                        "iDisplayLength": 50,
                        "bInfo": true,
                        "bFilter": true,
                        "bProcessing": true,
                        "bAutoWidth": false,
                        "aoColumnDefs": [
                            {
                                "bSortable": false,
                                "aTargets": [1]
                            }
                        ]
                    });
                }
            }
            catch (e) { };

            openDialogs();
        });

        function openViewOutputDialog(source) {
            $("#waitViewOutputDialog").dialog("open");
            $.post("InitExecuteReportViewOutput", { report: $(source).attr("id") })
            .done(function (data) {
                $("#waitViewOutputDialog").dialog("close");
                $("#viewOutputDialog").html(data);
                $("#viewOutputDialog").dialog("open");
                $("#viewOutputDialog").tooltip();
                $('#viewOutputDialog a').blur();
            });
        }

        function openUserPreferencesDialog() {
            $.post("InitUserPreferences")
            .done(function (data) {
                $("#userPreferencesDialog").html(data);
                $("#userPreferencesDialog").dialog("open");
                $("#userPreferencesDialog").tooltip();
                $('#userPreferencesDialog a').blur();
            });
        }

        function openDialogs() {
            if (@Helper.ToJS(!user.IsAuthenticated && repository.Security.PromptUserPassword))
            {
                $("#loginDialog").dialog("open");
        }
        else if (@Helper.ToJS(!string.IsNullOrEmpty(user.Error)))
        {
            $("#errorDialog").dialog("open");
        }
        }
    </script>
    <script type='text/javascript' src='~/Scripts/dataTables.js'></script>
</head>
<body>
    <img id="userInfo" src="~/Images/info.gif" title="@repository.TranslateWeb("User information and preferences")" class="information_icon" />

    @if (!string.IsNullOrEmpty(folderDetail.FolderPath) && Directory.Exists(folderDetail.FolderPath) && user.IsAuthenticated)
    {
        <table class="data_table">
            <thead>
                <tr>
                    <th>@repository.TranslateWeb("Report Name")</th>
                    <th></th>
                    <th>@repository.TranslateWeb("Last Modification")</th>
                    <th>@repository.TranslateWeb("Folder")</th>
                </tr>
            </thead>
            <tbody>
                @ShowReports(folderDetail.FolderPath, folderDetail.IsRecursive, repository, user)
            </tbody>
        </table>
    }

    <div id="viewOutputDialog" class="viewOutputDialog" title="@repository.TranslateWeb("Execute a report view or output")">
    </div>

    <div id="waitViewOutputDialog" class="viewOutputDialog" title="@repository.TranslateWeb("Execute a report view or output")">
        <input type="hidden" autofocus="autofocus" />
        <p style="font-weight: bold;">
            <img src="~/Images/Wait.gif" class="list_icon" />@repository.TranslateReport("Processing...")
        </p>
    </div>


    <div id="loginDialog" title="@(@repository.Configuration.WebProductName + " " + repository.TranslateWeb("Login"))">
        <form id="loginForm" action="Login" method="post" target="_top">
            <fieldset>
                <label for="name">@repository.TranslateWeb("User Name")</label>
                <input type="text" name="user_name" id="user_name" value="" class="text ui-widget-content ui-corner-all">
                <label for="password">@repository.TranslateWeb("Password")</label>
                <input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all">
            </fieldset>
            @if (!string.IsNullOrEmpty(user.Error))
            {
                <p style="color:red;"><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>@user.Error</p>
            }
        </form>
    </div>

    <div id="errorDialog" title="@repository.TranslateWeb("Error")">
        <input type="hidden" autofocus="autofocus" />
        <p style="color:red;"><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>@user.Error</p>
    </div>

    <div id="userPreferencesDialog" title="@repository.TranslateWeb("User information and preferences")">
    </div>
</body>
</html>

